<?php
/**
* @package   auto_occasion
* @subpackage essais
* @author    your name
* @copyright 2011 your name
* @link      http://www.yourwebsite.undefined
* @license    All rights reserved
*/

class BoDossierCtrl extends jController {
    
   public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
                            );
   /**
    * Première page affichée lorsqu'on clique sur dossier au niveau des essais du BO
    * Listing des dossier à gauche + possibilité d'ajout de dossier à droite
    */
    function index() {
        
        $rep = $this->getResponse('html') ;
       
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validate.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.core.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.widget.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.mouse.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.sortable.js');

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/ui.multiselect.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/infiniteCarousel.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/annonceDeposer.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/illustrationChapitres.css');

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/ajaxupload.3.5.js');
		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.localisation-min.js');
   		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/illustrationChapitres.js');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/infiniteCarousel.js');

        
        $rep->menusActifs = adminHtmlResponse::MENU_FICHES_GAMMES ;

        jClasses::inc('essais~dossierSrv') ;
        jClasses::inc('essais~dossierChapitreSrv') ;
        jClasses::inc('commun~common') ;
       
        $dossierId = $this->intParam('dossierId') ;
        $toParams['dossier']  = ($dossierId) ? common::chargeParId($dossierId, 'ao_dossier') : common::init('ao_dossier') ;
        $toParams['liste']    = dossierSrv::liste() ;
        $chapitre = ($dossierId) ? dossierChapitreSrv::getByDossierId($dossierId) : common::init('ao_dossier_chapitre') ;
        $tempC = array();
        if ($dossierId && sizeof($chapitre)) {
            foreach ($chapitre as $itemChap) {
                if ($itemChap->chap_img && strpos($itemChap->chap_img, ',')) {
                    $itemChap->chap_img = explode(',', $itemChap->chap_img);
                    $t = array();
                    foreach ($itemChap->chap_img as $img) {
                        if (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $img)) {
                            $t[] = PATH_ESSAI_IMG . $img;
                        } else if (is_file(JELIX_APP_WWW_PATH . $img)) {
                            $t[] = $img;
                        }
                    }

                    $itemChap->chap_img = $t;

                } elseif (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $itemChap->chap_img)) {
                    $itemChap->chap_img = PATH_ESSAI_IMG . $itemChap->chap_img;
                } elseif (is_file(JELIX_APP_WWW_PATH .  $itemChap->chap_img)) {
                    $itemChap->chap_img = $itemChap->chap_img;
                }
                $tempC[] = $itemChap;
            }
        }
        $toParams['chapitre'] = (sizeof($tempC)) ? $tempC : $chapitre ;

        if ( $dossierId && $toParams['dossier']->dossier_content) {
            $content = $toParams['dossier']->dossier_content;
            $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_DOSSIER_CHAPEAU_ . $content;
            if (strpos($content, ".txt") !== false) {
                //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                jClasses::inc('commun~toolsFile');
                $toParams['dossier']->dossier_content = toolsFile::get($filename);
            }
            $filenameImg = JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $toParams['dossier']->dossier_img;
            if (is_file($filenameImg)) {
                $toParams['dossier']->dossier_img = PATH_ESSAI_IMG . $toParams['dossier']->dossier_img;
            } 
        }

        // récupération des images
        if ($dossierId) {
            $photo = $const = array();
            if (strpos($toParams['dossier']->dossier_img, ',') !== false) {
                $tphotoChap = explode(',', $toParams['dossier']->dossier_img);
                foreach ($tphotoChap as $item) {
                    $withoutConst = 0;
                    if ($item && is_file(JELIX_APP_WWW_PATH . $item)) {
                        $withoutConst = 1;
                    }
                    $photo[] = $item;
                    $const[] = $withoutConst;
                }
            } else if ($toParams['dossier']->dossier_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $toParams['dossier']->dossier_img)) {
                $photo[] = $toParams['dossier']->dossier_img;
                $const[] = 0;
            } else if ($toParams['dossier']->dossier_img && is_file(JELIX_APP_WWW_PATH . $toParams['dossier']->dossier_img)) {
                $photo[] = $toParams['dossier']->dossier_img;
                $const[] = 1;
            }
            $toParams['dossier']->image        = $photo;
            $toParams['dossier']->withoutConst = $const;
        }

        $toParams['path_img'] = PATH_ESSAI_MIN . 'Dossier/';

        $rep->bodyTpl = 'essais~BoDossierEdit';
        $rep->body->assign($toParams) ;

        return $rep;
    }

   /**
    * Affiche la gestion des chapitres d'un dossier
    */
    function chapitre() {
        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_FICHES_GAMMES ;

         $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validate.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.core.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.widget.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.mouse.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.sortable.js');

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/ui.multiselect.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/infiniteCarousel.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/annonceDeposer.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/illustrationChapitres.css');

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/ajaxupload.3.5.js');
		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.localisation-min.js');
   		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/illustrationChapitres.js');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/infiniteCarousel.js');

        jClasses::inc('commun~common') ;
        jClasses::inc('essais~dossierSrv') ;
        jClasses::inc('essais~dossierChapitreSrv') ;
        jClasses::inc('commun~common') ;

        $chapitreId = $this->intParam('chapId') ;
        $toParams['dossierId']  = $this->intParam('dossierId') ;

        $chapitre   = ($chapitreId) ? common::chargeParId($chapitreId, 'ao_dossier_chapitre') : common::init('ao_dossier_chapitre');
        if (strpos($chapitre->chap_content, '.txt')) {
            jClasses::inc('commun~toolsFile');

            $path = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $chapitre->dossier_id . '/' . $chapitre->chap_content;
            $chapitre->chap_content = toolsFile::get($path);
        }
        $tempImgChap = array();
        // Vérifie si les chapitres sont séparés par des ','
        if (strpos($chapitre->chap_img, ',')) {
            // vérifier si les images existent % au path data/ puis les afficher
            $tabImageChap = explode(',', $chapitre->chap_img);

            if (sizeof($tabImageChap)) {
                foreach ($tabImageChap as $itemImg) {
                    $filepath = JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $itemImg;

                    if (is_file($filepath)) {
                        $tempImgChap[] = PATH_ESSAI_IMG . $itemImg;
                    } else if (is_file(JELIX_APP_WWW_PATH . $itemImg)) {
                        $tempImgChap[] = $itemImg;
                    }
                }
            }
        } else if (is_file(JELIX_APP_WWW_PATH . $chapitre->chap_img)){
            $tempImgChap[] = $chapitre->chap_img;
        } else if (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $chapitre->chap_img)){
            $tempImgChap[] = PATH_ESSAI_IMG . $chapitre->chap_img;
        }
        //print_r($tempImgChap);die();
        $toParams['chapitre1']      = $chapitre;
        $toParams['tempImgChap']   = $tempImgChap;


        // Bloc left
       
        $dossierId = $this->intParam('dossierId') ;
        $toParams['dossier']  = ($dossierId) ? common::chargeParId($dossierId, 'ao_dossier') : common::init('ao_dossier') ;
        $toParams['liste']    = dossierSrv::liste() ;
        $chapitre = ($dossierId) ? dossierChapitreSrv::getByDossierId($dossierId) : common::init('ao_dossier_chapitre') ;
        $tempC = array();
        if ($dossierId && sizeof($chapitre)) {
            foreach ($chapitre as $itemChap) {
                if ($itemChap->chap_img && strpos($itemChap->chap_img, ',')) {
                    $itemChap->chap_img = explode(',', $itemChap->chap_img);
                    $t = array();
                    foreach ($itemChap->chap_img as $img) {
                        if (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $img)) {
                            $t[] = PATH_ESSAI_IMG . $img;
                        } else if (is_file(JELIX_APP_WWW_PATH . $img)) {
                            $t[] = $img;
                        }
                    }

                    $itemChap->chap_img = $t;

                } elseif (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $itemChap->chap_img)) {
                    $itemChap->chap_img = PATH_ESSAI_IMG .'/'. $itemChap->chap_img;
                } elseif (is_file(JELIX_APP_WWW_PATH .  $itemChap->chap_img)) {
                    $itemChap->chap_img = $itemChap->chap_img;
                }
                $tempC[] = $itemChap;
            }
        }

        $toParams['chapitre'] = (sizeof($tempC)) ? $tempC : $chapitre ;

        if ( $dossierId && $toParams['dossier']->dossier_content) {
            $content = $toParams['dossier']->dossier_content;
            $filename = JELIX_APP_WWW_PATH . PATH_ESSAI_DOSSIER_CHAPEAU_ . $content;
            if (strpos($content, ".txt") !== false) {
                //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                jClasses::inc('commun~toolsFile');
                $toParams['dossier']->dossier_content = toolsFile::get($filename);
            }
            $filenameImg = JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $toParams['dossier']->dossier_img;
            if (is_file($filenameImg)) {
                $toParams['dossier']->dossier_img = PATH_ESSAI_IMG . $toParams['dossier']->dossier_img;
            } 
        }

        // récupération des images
        if ($dossierId) {
            $photo = $const = array();
            if (strpos($toParams['dossier']->dossier_img, ',') !== false) {
                $tphotoChap = explode(',', $toParams['dossier']->dossier_img);
                foreach ($tphotoChap as $item) {
                    $withoutConst = 0;
                    if ($item && is_file(JELIX_APP_WWW_PATH . $item)) {
                        $withoutConst = 1;
                    }
                    $photo[] = $item;
                    $const[] = $withoutConst;
                }
            } else if ($toParams['dossier']->dossier_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $toParams['dossier']->dossier_img)) {
                $photo[] = $toParams['dossier']->dossier_img;
                $const[] = 0;
            } else if ($toParams['dossier']->dossier_img && is_file(JELIX_APP_WWW_PATH . $toParams['dossier']->dossier_img)) {
                $photo[] = $toParams['dossier']->dossier_img;
                $const[] = 1;
            }
            $toParams['dossier']->image        = $photo;
            $toParams['dossier']->withoutConst = $const;
        }

        $toParams['path_img'] = PATH_ESSAI_MIN . 'Dossier/';
        // --- Bloc left


        $rep->bodyTpl = 'essais~BoDossierChapitre';

        $rep->body->assign($toParams) ;


        return $rep;
    }

    /**
    * Sauvegarder un dossier
    */
    function sauvegarder() {
        $rep = $this->getResponse('redirect');
        
        jClasses::inc('essais~dossierSrv');
        jClasses::inc('commun~common') ;


        $dossier                    = common::init('ao_dossier');
        $dossier->dossier_id        = $this->intParam('dossier_id', 0);
        $dossier->dossier_name      = $this->stringParam('dossier_name', '');
        $dossier->dossier_content   = $this->stringParam('dossier_content', '');
        $dossier->dossier_img       = $this->stringParam('photo-annonce', '');

        if ($dossier->dossier_id) {
            dossierSrv::modifier($dossier);
        } else {
            dossierSrv::ajouter($dossier);
        }
        
        $rep->action = 'essais~BoDossier:index';
        $rep->params = array('dossierId' => $dossier->dossier_id) ;

        return $rep;
    }
    /**
    * Sauvegarder un chapitre d'un dossier
    */
    function saveChapitre() {
        $rep = $this->getResponse('redirect');
        
        jClasses::inc('essais~dossierSrv');
        jClasses::inc('commun~common') ;


        $chapitre                 = common::init('ao_dossier_chapitre');
        $chapitre->chap_id        = $this->intParam('chap_id', 0);
        $chapitre->dossier_id     = $this->intParam('dossierId', 0);
        $chapitre->chap_name      = $this->stringParam('chap_name', '');
        $chapitre->chap_content   = $this->stringParam('chap_content', '');
        $chapitre->chap_img       = $this->stringParam('photo-annonce', '');

        if ($chapitre->chap_id) {
            dossierSrv::modifierChapitre($chapitre);
        } else {
            $chapitre->chap_id = dossierSrv::ajouterChapitre($chapitre);
        }
        
        $rep->action = 'essais~BoDossier:chapitre';
        $rep->params = array('dossierId' => $chapitre->dossier_id, 'chapId'=>$chapitre->chap_id) ;

        return $rep;
    }

    /**
    * Supprime un dossier
    */
    function supprimer() {
        $rep = $this->getResponse('redirect');
        jClasses::inc('commun~common');

        $id = $this->intParam('id');
        common::supprimer($id, 'ao_dossier');

        $rep->action = 'essais~BoDossier:index';

        return $rep;
    }

    /**
    *
    */
    function reOrder() {
        
        jClasses::inc('essais~dossierSrv') ;
        
        $postedParams = $this->params() ;
        
        $rep = $this->getResponse('text') ;
        $rep->content = dossierSrv::reOrder($postedParams) ;

        return $rep;
    }
    /**
    *
    */
    function reOrderChap() {
        
        jClasses::inc('essais~dossierSrv') ;
        
        $postedParams = $this->params() ;
        
        $rep = $this->getResponse('text') ;
        $rep->content = dossierSrv::reOrderChap($postedParams) ;

        return $rep;
    }
    

}